package com.copm.ifm.base.auth;

import com.copm.ifm.base.basic.util.AuthUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 鉴权拦截器
 *
 * @author zzf
 * @date 2020/7/22 9:50
 */
@Aspect
@Component
public class AuthenticationInterceptor {


    /**
     * 拦截RequiresPermissions注解的方法
     */
    @Pointcut("@annotation(com.copm.ifm.base.auth.RequiresPermissions)")
    public void authentication() {
    }


    /**
     * 鉴权
     */
    @Before("authentication()")
    public void doAuthentication(JoinPoint joinPoint) throws NoSuchMethodException {
        //获取标记
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature;
        if (signature instanceof MethodSignature) {
            methodSignature = (MethodSignature) signature;
        } else {
            return;
        }
        //通过标记反射获取方法信息
        Object target = joinPoint.getTarget();
        Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        //获取方法头上的注解信息
        RequiresPermissions annotation = currentMethod.getAnnotation(RequiresPermissions.class);
        if (AuthUtil.checkRole(annotation.menu().getName(), annotation.option().getName())) {

        } else {

        }
    }


}
